package com.ac.app.web.action.pick;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.ac.app.dm.*;
import com.ac.app.service.PickManager;
import com.ac.app.web.form.SearchForm;
import com.ac.app.web.view.PkRecordView;
import com.caac.core.util.Page;
import com.caac.core.web.action.BasePageViewAction;
import com.caac.core.web.action.PageViewForward;
import com.caac.core.web.form.BaseForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2008</p>
 * <p>Company: AgileCentury </p>
 * @author <a href="mailto:ac@agilecentury.com">Agile</a>
 * @version 1.0
 */

public class Search1Action extends BasePageViewAction {
    protected PageViewForward doPageViewExecute(ActionMapping mapping,
                                                BaseForm form,
                                                HttpServletRequest request,
                                                HttpServletResponse response,
                                                int pageNo,
                                                int pageSize) throws Exception {
        SearchForm myForm = (SearchForm) form;
        PickManager pm = (PickManager) this.getBean("pickManager");
        Page page = null;
        if (myForm.isFirst()) {
            if (myForm.getPbDm() == null) {
                myForm.setPbDm(pm.getPickBase());
                List peList = pm.getPickEntrys();
                for (int i = 0; i < peList.size(); i++) {
                    myForm.addEntry((PickEntry) peList.get(i));
                }
            }
            int iAllRow = 0;
            myForm.getPrList().clear();
            myForm.getPrCurList().clear();

            if (myForm.getKeyword() != null && !"".equals(myForm.getKeyword())) {
                List puList = pm.getPickUrlsByDays(myForm.getValDays());
                int iRow = 0;
                for (int i = 0; i < puList.size(); i++) {
                    PickUrl puDm = (PickUrl) puList.get(i);
                    PickEntry peDm = (PickEntry) myForm.getEntrys().getChild(
                            puDm.getEntryId());
                    if ("99".equals(myForm.getMeta())
                            || peDm.getLanguageTp().equals(myForm.getMeta())) {
                        PickRecord prDm = pm.getPickRecord(peDm, puDm, myForm
                                .getPbDm().getShotPath(), myForm.getKeyword());
                        if (prDm != null) {
                            iAllRow++;
                            myForm.addPr(prDm);
                            if (iRow <= pageSize) {
                                PkRecordView prv = new PkRecordView();
                                prv.setPickRecord(prDm);
                                myForm.addPrCur(prv);
                                iRow++;
                            }
                        }
                    }
                }
            }

            page = new Page(iAllRow, pageNo, pageSize);
            myForm.setFirst(false);
        } else {
            page = new Page(myForm.getPrList().size(), pageNo, pageSize);
            List prList = myForm.getPrList();
            myForm.getPrCurList().clear();
            for (int i = page.getRowFrom() - 1; i < page.getRowTo(); i++) {
                PkRecordView prv = new PkRecordView();
                prv.setPickRecord((PickRecord) prList.get(i));
                myForm.addPrCur(prv);
            }
        }

        return new PageViewForward(forward(mapping), myForm.getPrCurList(),
                page, null, null, null);
    }
}
